use sea_orm_migration::prelude::*;

use crate::idents::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Device::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Device::Id)
                            .big_integer()
                            .primary_key()
                            .auto_increment(),
                    )
                    .col(ColumnDef::new(Device::DeviceId).binary().not_null())
                    .col(ColumnDef::new(Device::Seqno).big_integer())
                    .col(
                        ColumnDef::new(Device::IsMostRecentKnown)
                            .boolean()
                            .not_null()
                            .default(false),
                    )
                    .col(
                        ColumnDef::new(Device::IsEquivocation)
                            .boolean()
                            .not_null()
                            .default(false),
                    )
                    .col(ColumnDef::new(Device::Record).binary().not_null())
                    .to_owned(),
            )
            .await?;

        // Needs to be split out because of how the syntax gets generated.
        manager
            .create_index(
                Index::create()
                    .name("IDX_device_on_device_id_and_seqno")
                    .table(Device::Table)
                    .col(Device::DeviceId)
                    .col(Device::Seqno)
                    .if_not_exists()
                    .to_owned(),
            )
            .await?;

        manager
            .create_table(
                Table::create()
                    .table(UserDevice::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(UserDevice::Id)
                            .big_integer()
                            .primary_key()
                            .auto_increment(),
                    )
                    .col(ColumnDef::new(UserDevice::UserId).integer().not_null())
                    .col(ColumnDef::new(UserDevice::DeviceId).binary().not_null())
                    .col(ColumnDef::new(UserDevice::Added).timestamp().not_null())
                    .index(
                        Index::create()
                            .name("IDX_user_device_on_owner_user_id")
                            .if_not_exists()
                            .col(UserDevice::UserId)
                            .unique(),
                    )
                    .index(
                        Index::create()
                            .name("IDX_user_device_on_device_id")
                            .if_not_exists()
                            .col(UserDevice::DeviceId)
                            .unique(),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("FK_user_device_user_id_to_user_id")
                            .from(UserDevice::Table, UserDevice::UserId)
                            .to(User::Table, User::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(Device::Table).to_owned())
            .await?;

        manager
            .drop_table(Table::drop().table(UserDevice::Table).to_owned())
            .await?;

        Ok(())
    }
}
